package com.cfuture08.eweb4j.component.dwz;

import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.List;

import com.cfuture08.util.ReflectUtil;
import com.cfuture08.util.StringUtil;

/**
 * DWZ list页面数据模板
 * 
 * @author cfuture.aw
 * 
 */
public class ListTemp {
	private String addJsp;
	private String delAction;
	private String showOneAction;
	private String showAllAction;
	private String tableHeader;
	private String tableBodyTr;
	private String checkBox;

	public <T> ListTemp(String addJsp, String delAction, String showOneAction,
			String showAllAction, List<T> list) {
		if (list != null && list.size() > 0) {
			this.addJsp = addJsp;
			this.delAction = delAction;
			this.showOneAction = showOneAction;
			this.showAllAction = showAllAction;
			this.checkBox = list.get(0).getClass().getSimpleName()+System.currentTimeMillis();
			StringBuilder tableHeader = new StringBuilder();
			try {
				ReflectUtil ru = new ReflectUtil(list.get(0).getClass());
				for (String name : ru.getFieldsName()) {
					tableHeader.append("<th width='200'><b>");
					tableHeader.append(name);
					tableHeader.append("</b></th>");
				}
				this.tableHeader = tableHeader.toString();
				StringBuilder tableBodyTr = new StringBuilder();

				for (Iterator<T> it = list.iterator(); it.hasNext();) {
					ru = new ReflectUtil(it.next());
					Method m = ru.getMethod("getId");
					if (m != null) {
						Object idV = m.invoke(ru.getObject());
						tableBodyTr.append("<tr rel='");
						tableBodyTr.append(idV);
						tableBodyTr.append("'>");
						tableBodyTr.append("<td><input class='");
						tableBodyTr.append(this.checkBox).append("'");
						tableBodyTr.append(" type='checkbox' value='");
						tableBodyTr.append(idV);
						tableBodyTr.append("' /></td>");
					}

					for (String name : ru.getFieldsName()) {

						Method m2 = ru.getMethod("get"
								+ StringUtil.toUpCaseFirst(name));
						if (m2 != null) {
							tableBodyTr.append("<td>");
							if ("password".equalsIgnoreCase(name)) {
								tableBodyTr.append("******");
							} else {
								tableBodyTr.append(m2.invoke(ru.getObject()));
							}
							tableBodyTr.append("</td>");
						}

					}

					tableBodyTr.append("</tr>");
					this.tableBodyTr = tableBodyTr.toString();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public String getAddJsp() {
		return addJsp;
	}

	public void setAddJsp(String addJsp) {
		this.addJsp = addJsp;
	}

	public String getDelAction() {
		return delAction;
	}

	public void setDelAction(String delAction) {
		this.delAction = delAction;
	}

	public String getShowOneAction() {
		return showOneAction;
	}

	public void setShowOneAction(String showOneAction) {
		this.showOneAction = showOneAction;
	}

	public String getShowAllAction() {
		return showAllAction;
	}

	public void setShowAllAction(String showAllAction) {
		this.showAllAction = showAllAction;
	}

	public String getTableHeader() {
		return tableHeader;
	}

	public void setTableHeader(String tableHeader) {
		this.tableHeader = tableHeader;
	}

	public String getTableBodyTr() {
		return tableBodyTr;
	}

	public void setTableBodyTr(String tableBodyTr) {
		this.tableBodyTr = tableBodyTr;
	}

	public String getCheckBox() {
		return checkBox;
	}

	public void setCheckBox(String checkBox) {
		this.checkBox = checkBox;
	}
}
